<?php
namespace Admin\Controller;

use Admin\Controller\AdminController;
use Admin\Model\AdverModel;
use Admin\Model\AdverPlaceModel;

/**
 * Description of RoleController
 *
 * @author yaoyunhua
 * @todo 轮播模块
 * @menu 轮播管理
 * @menuAuto advList
 * @order 200
 * @icon content.png
 */

class ContentController extends AdminController
{
    //put your code here
    private $adv;
    private $advp;
    private $advId;
    private $advpId;
    private $advObj;

    function __construct() {
        parent::__construct();
        $this->adv =  new AdverModel();
        $this->advp =  new AdverPlaceModel();
        //$this->userOrg = new UserOrgModel();
        $this->advId = I('advId','');
        $this->advpId = I('advpId','');
        $this->advObj = array(
            'news'=>'新闻',
            'activity'=>'活动',
            //'house'=>'楼宇'
        );
    }

    /**
     * @author yaoyunhua
     * @todo 轮播列表
     * @menu 轮播内容
     * @order 30
     */
    public function advList(){
        $advList = $this->adv->getAdvList(array('status'=>1));
        foreach ($advList['list'] as &$v){
            switch ($v['adv_object']){
                case 'news':
                case 'activity':
                    $rs = M("news")->where("id = ".$v['adv_object_id'])->field("title")->find();
                    $v['adv_obj_name'] = $rs['title'];
                    
                    break;
//                case 'house':
//                    $rs = M("house")->where("id = ".$v['adv_object_id'])->field("name")->find();
//                    $v['adv_obj_name'] = $rs['name'];
//                    break;
            }
            $v['adv_object_name'] = $this->advObj[$v['adv_object']];
        }
      
        $this->assign("advList",$advList);
        $this->display('advList');
    }

    /**
     * @todo 添加轮播
     */
    public function addAdv(){
        $advPlaces = $this->advp->getadvPlaceList(array('status'=>1));//广告位列表数据
        $this->assign("advPlaces",$advPlaces);
        $this->assign("advObj",$this->advObj);
        $this->display('newAdv');
    }
    /**
     * @author  yaoyunhua
     * @todo 添加轮播操作
     */
    public function doAddAdv(){
        $data = $this->adv->create();
        if($data){
            $data['add_time'] = time();
            $data['user_id'] = $_SESSION['userId'];
            $data['status'] = 1;
            $rs = $this->adv->add($data);
            if($rs !== false ){
                $this->ajaxReturn($rs);
            }
        }else{
            $this->ajaxReturn($this->adv->getDbError());
        }
    }
    /**
     * @author yaoyunhua
     * @todo 删除轮播
     */
    public function delAdv(){
        $data = array('status'=>0);
        $res = $this->adv->delAdv($this->advId,$data);
        $this->ajaxReturn($res);
    }

    /**
     * @todo 编辑轮播
     */
    public function editAdv(){
        $advPlaces = $this->advp->getadvPlaceList(array('status'=>1));//广告位列表数据
        $res = $this->adv->getAdvInfoById($this->advId);
        $this->assign("advInfo",$res);
        $this->assign("advPlaces",$advPlaces);
        $this->assign("advObj",$this->advObj);
        $this->display("editAdv");
    }

    /**
     * @todo  编辑轮播操作
     */
    public function saveAdv(){
        $data = $this->adv->create();
        $data['edit_time'] = time();
        if($data){
            $rs = $this->adv->where("id=".$this->advId)->save($data);
            if($rs !== false ){
                $this->ajaxReturn($rs);
            }
        }/*else{
            $this->ajaxReturn($this->role->getDbError());
        }*/
    }

    /**
     * @author yaoyunhua
     * @todo 轮播位列表
     * @menu 轮播位管理
     * @order 40
     */
    public function advPlaceList(){
        $advPlaceList = $this->advp->getadvPlaceList(array('status'=>1));
        $this->assign("advPlaceList",$advPlaceList);
        $this->display('advPlaceList');
    }
    /**
     * @author  yaoyunhua
     * @todo 添加轮播位
     */
    public function addAdvPlace(){
        $data = $this->advp->create();
        if($data){
            $data['add_time'] = time();
            $data['status'] = 1;
            //$data['add_time'] = time();
            $rs = $this->advp->add($data);
            if($rs !== false ){
                $this->ajaxReturn($rs);
            }
        }else{
            $this->ajaxReturn($this->adv->getDbError());
        }
    }
    /**
     * @author yaoyunhua
     * @todo 删除轮播位
     */

    public function delAdvPlace(){
        $data = array('status'=>0);
        $res = $this->advp->delAdvp($this->advpId,$data);
        $this->ajaxReturn($res);
    }

    /**
     * @todo 编辑轮播位
     */
    public function editAdvPlace(){
        $res = $this->advp->getAdvPlaceInfoById($this->advpId);
        $this->assign("advPlaceInfo",$res);
        $this->display("editAdvPlace");
    }
    /**
     * @todo  编辑轮播位操作
     */
    public function saveAdvPlace(){
        if($this->advp->create()){
            $rs = $this->advp->where("id=".$this->advpId)->save();
            if($rs !== false ){
                $this->ajaxReturn($rs);
            }
        }else{
            $this->ajaxReturn($this->advp->getDbError());
        }
    }
    
    function getAdvObjectList(){
        $type = I('type');
        switch ($type){
            case "news":
                $res = M("news")->where(" status = 1 and cate_id in (1,2) ")->field("id,title name")->select();
                break;
            case "activity":
                $res = M("news")->where(" status = 1 and cate_id =3 ")->field("id,title name")->select();
                break;
//            case "house":
//                $res = M("house")->where("status = 1")->field("id,name")->select();
//                break;
        }
        $this->ajaxReturn($res);
    }
}